#!/usr/bin/python
# -*- coding: utf-8 -*-

from cloud_server.test.ten_get_dns_record_list import SetDNSRecordParam
from urllib import parse
from urllib import request

Ten_SecretId = 'AKIDPaUoFZKR5j7FrNSCEzUjiTagX9WdQBdw'
Ten_SecretKey = 'RebIkxuY2VjhPs64uhSzHmHYDftrb8UD'

req = SetDNSRecordParam()

req.set_domain('leecq.xyz')
req.set_SecretId(Ten_SecretId)

param = req.get_param()
print(param)

def pin_jie(dic):
    def sortedDictValues1(adict):  # 对 字典排序
        keys = list(adict.keys())
        keys = sorted(keys)
        _d = {}
        for k in keys:
            _d[k] = adict[k]
        return _d


    # 拼接请求参数
    param = sortedDictValues1(dic)
    s = ''
    for k, v in param.items():
        s += k + '=' + str(v) + '&'
    s = s[:len(s)-1]
    print(s)
    return s

s = pin_jie(param)
# 拼接 host path
s = 'GET' + req.HOST + req.PATH + s
print(s)

# sha1 得到 加密字符串
from hashlib import sha1
import hmac
import base64

my_sign = hmac.new(s.encode(), Ten_SecretKey.encode(), sha1).digest()
my_sign = base64.b64encode(my_sign)

# my_sign = str(my_sign, encoding='utf8')

# my_sign = my_sign[2:]
# my_sign = my_sign[:len(my_sign)-1]

print(my_sign)

# 最终请求URL
req.set_Signature(my_sign)

url_ = parse.urlencode(req.get_param())

# s = pin_jie(request.get_param())
s = 'https://' + req.HOST + req.PATH + url_
print(s)

print('*'* 20)
# Url 编码

# url = parse.urlencode(s)
print('URL:',s)
data = request.urlopen(s)
print(data)
print(str(data.read(), encoding='utf8'))
